
#define LightSensor A0 //定义A0口为光传感器LightSensor
#define MoistureSensor A1 //定义A1口为土壤湿度传感器MoistureSensor

#define RED 9 //定义D9为RGB_LED灯的红色
#define GREEN 10 //定义D10为RGB_LED灯的绿色
#define BLUE 11 //定义D11为RGB_LED灯的蓝色
#define WaterPump 4 //定义D4口为水泵WaterPump
#define BUZZER 3 //定义D3为蜂鸣器

//初始化
void setup(){
  //设定传感器 --> 输入设备
  pinMode(LightSensor, INPUT); //光传感器初始化
  pinMode(MoistureSensor, INPUT); //土壤湿度传感器初始化
  //设定执行器 --> 输出设备
  pinMode(RED, OUTPUT); //灯带初始化-红色
  pinMode(GREEN, OUTPUT); //灯带初始化-绿色
  pinMode(BLUE, OUTPUT); //灯带初始化-蓝色
  pinMode(WaterPump, OUTPUT); //水泵初始化
  pinMode(BUZZER,OUTPUT); //蜂鸣器初始化
  Serial.begin(9600); //初始化串口监视器 --> 波特率设置为9600

  digitalWrite(RED, 0);
  digitalWrite(GREEN, 0);
  digitalWrite(BLUE, 0);
  delay(200);
  
}

//信号读取
void signalRead(){
  Serial.print("土壤湿度数值（数据越大土壤越干燥）："); //打印土壤湿度数据提示信息
  Serial.println(analogRead(MoistureSensor)); //读取并打印土壤湿度传感器的模拟信号数值
  Serial.print("光传感器数值（数据越大环境光越暗）："); //打印光传感器数据提示信息
  Serial.println(analogRead(LightSensor)); //读取并打印光传感器的模拟信号数值
  Serial.println("------------------------------"); //打印分隔符
  delay(1000);
  
}

void buzzerWork(){ //蜂鸣器工作函数
  tone(BUZZER,523,200);   //将蜂鸣器模块对应端口设为高电平
  delay(200);
  noTone(BUZZER);
  delay(200);
}

//决策
void lightJudge(){
  
  if(analogRead(LightSensor)>0 && analogRead(LightSensor)<=550){
    digitalWrite(RED, 1);
    digitalWrite(GREEN, 1);
    digitalWrite(BLUE, 1);
  }
  else if(analogRead(LightSensor)>550 && analogRead(LightSensor)<=800){
    analogWrite(RED, 1);
    analogWrite(GREEN, 1);
    analogWrite(BLUE, 1);
  }
  else if(analogRead(LightSensor)>800){
    analogWrite(RED, 200);
    analogWrite(GREEN, 255);
    analogWrite(BLUE, 1);
  }


  
}

void moistureJudge(){
  if(analogRead(MoistureSensor)>500){
    digitalWrite(WaterPump, 1);
  }
  else if(analogRead(MoistureSensor)<500){
    digitalWrite(WaterPump, 0);
  }
}


void loop(){
  signalRead(); //执行信号读取
  lightJudge();
  moistureJudge();
}
